<template>
  <el-row type="flex" justify="center">
    <el-col :xl="6" :lg="7">
      <h2>欢迎使用人力资源管理系统</h2>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password"></el-input>
        </el-form-item>      
        <el-form-item label="验证码" prop="code">
          <el-input v-model="loginForm.code" style="width:150px; float:left;"></el-input>
          <el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import request from '@/util/request.js'
import qs from 'qs'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

export default {
  data() {
    return {
      loginForm: {
        username: '', // 账号
        password: '', // 密码
        code: '', // 验证码
        captchaKey: '' // 验证码key，用于后端获取真正的验证码
      },
      captchaImg: '',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
          if (valid) {
            NProgress.start() // 进度条开始
            // 发送登录请求，因为request使用的是json格式，所以这里使用原始axios
            axios.post('/login', qs.stringify(this.loginForm)).then(res => {
              NProgress.done()  // 进度条结束
              const result = res.data
              // 登录成功
              if(result.code === 200) {
                // 保存jwt
                const jwt = res.headers['authorization']
                // 保存用户jwt
                this.$store.dispatch('setToken', jwt).then(res=>{
                  // 跳转
                  this.$router.push('/index')
                })
              }
              // 登录失败
              else {
                ElMessage.error(result.msg)
              }
            })
            
          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getCaptcha() {
      request.get('/captcha').then(res=>{
        // console.log(res)
        this.loginForm.captchaKey = res.data.captchaKey
        this.captchaImg = res.data.captchaImg
        this.loginForm.code = ''
      })
    }
  }
}
</script>

<style scoped>

  .el-row {
    background-color: #fafafa;
    height: 100%;
    display: flex;
    align-items: center;
    text-align: center;
  }

  .el-form-item {
    width: 380px;
  }

  .el-divider {
    height: 200px;
  }

  .captchaImg {
    float: left;
    margin-left: 8px;
    border-radius: 4px;
  }
</style>